package cn.xs.order.dao.impl;


import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.xs.core.dao.impl.BaseDaoImpl;
import cn.xs.goods.entity.Good;
import cn.xs.order.dao.OrderDao;
import cn.xs.order.entity.Order;
import cn.xs.order.entity.OrderItem;

public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao {

	
	/**
	 * 1.保存order
	 * 2.遍历保存orderItem
	 */
	public void save(Order order) {
		try{
			//保存order
			String sql = "insert into t_order values(?,?,?,?,?,?,?,?,?);";
			Object[] params = {order.getId(),order.getConsignee(),order.getAddress(),
							order.getTime(),order.getPhone(),order.getSumPrice(),order.getUser_id(),order.getReadme(),
								order.getStatus()};
			getQueryRunner().update(sql, params);
			
			//保存orderItem
			sql = "insert into t_orderItem values(?,?,?,?,?);";
			List<OrderItem> orderItems = order.getOrderItems();
			for(OrderItem item : orderItems){
				params = new Object[]{item.getId(),item.getGood_id(),item.getQuantity(),item.getPrice(),item.getOrder_id()};
				getQueryRunner().update(sql, params);
			}
			
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	
	
	@Override
	public Order find(String id) {
		Order order = super.find(id);
		List<OrderItem> orderItems = findOrderItemByOrder(id);
		order.setOrderItems(orderItems);
		return order;
	}

	public List<OrderItem> findOrderItemByOrder(String orderid) {
		try{
			String sql = "select * from t_orderItem where order_id = '"+orderid+"';";
			List<OrderItem> result = (List<OrderItem>) getQueryRunner().query(sql, new BeanListHandler(OrderItem.class));
			//填充里面的good
			sql = "select * from t_good where id = ?;";
			for(OrderItem item : result){
				Object[] params = new Object[]{item.getGood_id()};
				Good good = (Good) getQueryRunner().query(sql, new BeanHandler(Good.class),params);
				item.setGood(good);
			}
			return result;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Order> findAll() {
		List<Order> results = super.findAll();
		for(Order o : results){
			o.setOrderItems(findOrderItemByOrder(o.getId()));
		}
		return results;
	}

	public void setOrderStatus(String orderId, String status) {
		try{
			//这段代码bug解决不了,为了实现不得已采用下面的烂方法
//			String sql = "updata t_order set status = ? where id = ?;";
//			Object[] params = {status,orderId};
//			getQueryRunner().update(sql, params);
			
			//这是本人写过的最垃圾的代码
			Order order = super.find(orderId);
			delete(orderId);
			order.setStatus(status);
			simplesave(order);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * ..烂方法
	 * 这个方法不会保存订单项,而且也不提供给外界使用
	 * @param order
	 */
	private void simplesave(Order order){
		try{
			//保存order
			String sql = "insert into t_order values(?,?,?,?,?,?,?,?,?);";
			Object[] params = {order.getId(),order.getConsignee(),order.getAddress(),
							order.getTime(),order.getPhone(),order.getSumPrice(),order.getUser_id(),order.getReadme(),
								order.getStatus()};
			getQueryRunner().update(sql, params);
			
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public List<Order> findOrdersByUserId(String userid) {
		try{
			String sql = "select * from t_order where user_id = '"+userid+"' order by time desc;";
			List<Order> orders = (List<Order>) getQueryRunner().query(sql, new BeanListHandler(Order.class));
			//设置orderitem
			for(Order o : orders){
				o.setOrderItems(findOrderItemByOrder(o.getId()));
			}
			return orders;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Order> findAllByStatus(String status) {
		try{
			String sql = "select * from t_order where status = '"+status+"';";
			List<Order> results = (List<Order>) getQueryRunner().query(sql, new BeanListHandler(Order.class));
			for(Order o : results){
				o.setOrderItems(findOrderItemByOrder(o.getId()));
			}
			return results;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	
}
